package io.renren.modules.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.manage.dao.DataRestoreDao;
import io.renren.modules.manage.dao.PayLogCopyDao;
import io.renren.modules.manage.entity.DataRestoreEntity;
import io.renren.modules.manage.entity.PayLogCopyEntity;
import io.renren.modules.manage.service.DataRestoreService;
import io.renren.modules.seller.dao.PayLogDao;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.PayLogEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("dataRestoreService")
@Transactional(rollbackFor = Exception.class)
public class DataRestoreServiceImpl extends ServiceImpl<DataRestoreDao, DataRestoreEntity> implements DataRestoreService {

    @Autowired
    private DataRestoreDao dataRestoreDao;
    @Autowired
    private PayLogCopyDao payLogCopyDao;
    @Autowired
    private PayLogDao payLogDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DataRestoreEntity> page = this.page(
                new Query<DataRestoreEntity>().getPage(params),
                new QueryWrapper<DataRestoreEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void copy() {
        List<DataRestoreEntity> list = this.list();
        Date date = new Date();
        if (list.isEmpty()) {
            List<PayLogEntity> payLogEntities = payLogDao.selectList(null);
            payLogCopyDao.batchInsert(payLogEntities);
            DataRestoreEntity entity = new DataRestoreEntity();
            SysUserEntity userEntity = ShiroUtils.getUserEntity();
            entity.setCopyId(userEntity.getUserId());
            entity.setCopyTime(date);
            entity.setCopyName(userEntity.getUsername());
            entity.setTableName("pay_log");
            this.save(entity);
        } else {
            DataRestoreEntity entity = list.iterator().next();
            Date copyTime = entity.getCopyTime();
            Wrapper<PayLogEntity> wrapper = new QueryWrapper<PayLogEntity>().ge("create_time", copyTime);
            List<PayLogEntity> payLogEntities = payLogDao.selectList(wrapper);
            payLogCopyDao.batchInsert(payLogEntities);
            entity.setCopyTime(date);
            entity.setCopyId(ShiroUtils.getUserId());
            this.updateById(entity);
        }
    }

    @Override
    public void restore(Long id) {
        List<DataRestoreEntity> list = this.list();
        if (list.isEmpty()) {
            throw new RRException("找不到备份记录");
        }
        List<PayLogCopyEntity> payLogCopyEntities = payLogCopyDao.selectList(null);
        if (payLogCopyEntities.isEmpty()) {
            throw new RRException("该表还没有数据不用备份");
        }
        List<PayLogCopyEntity> arrayList = new ArrayList<>(512);
        for (PayLogCopyEntity entity1 : payLogCopyEntities) {
            String outTradeNo = entity1.getOutTradeNo();
            PayLogEntity payLogEntity = payLogDao.selectById(outTradeNo);
            if (payLogEntity == null) {
                arrayList.add(entity1);
            }
        }
        payLogDao.batchInsert(arrayList);
        DataRestoreEntity entity = list.iterator().next();
        SysUserEntity sysUser = ShiroUtils.getUserEntity();
        entity.setRestoreId(sysUser.getUserId());
        entity.setRestoreName(sysUser.getUsername());
        entity.setRestoreTime(new Date());
        this.updateById(entity);
    }

    @Override
    public PageResult findPage(DataRestoreEntity dataRestoreEntity, String page, String limit) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", limit);

        String tableName = dataRestoreEntity.getTableName();
        IPage<DataRestoreEntity> iPage = this.page(
                new Query<DataRestoreEntity>().getPage(params),
                new QueryWrapper<DataRestoreEntity>()
                        .eq(StringUtils.isNotBlank(tableName), "table_name", tableName)
        );
        return new PageResult(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public PageResult findPage(Map<String, Object> params) {
        IPage<DataRestoreEntity> page = this.page(
                new Query<DataRestoreEntity>().getPage(params),
                new QueryWrapper<DataRestoreEntity>()
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

}
